﻿using System;
using System.IO;
using System.Reflection;
using log4net;
using log4net.Config;

namespace Tk.Soc.Share.Common.Helpers
{
    public class LogHelper
    {
        private static ILog _log;

        private static LogHelper _logger;

        /// <summary>
        /// 日志记录
        /// </summary>
        public static LogHelper Logger
        {
            get
            {
                return _logger;
            }
        }

        public static void Initialize(string log4jPath)
        {
            if (_logger == null)
            {
                _logger = new LogHelper();
                XmlConfigurator.Configure(new FileInfo(log4jPath));
                _log = LogManager.GetLogger("RollingLogFileAppender");
            }
        }

        public void Error(string message)
        {
            _log.Error(message);
        }

        public void Error(string message, Exception ex)
        {
            var msg = string.Format("消息：{0}ExMessage信息:{1},ExStackTrace堆栈{2},{3}", message, ex.Message, ex.StackTrace, (ex.InnerException == null ? "" : ex.InnerException.Message));
            _log.Error(msg);
        }

        public void Fatal(string message)
        {
            _log.Fatal(message);
        }

        public void Fatal(string message, Exception ex)
        {
            _log.Fatal(message, ex);
        }

        public void Warn(string message)
        {
            _log.Warn(message);
        }

        public void Warn(string message, Exception ex)
        {
            _log.Warn(message, ex);
        }

        public void Info(string message)
        {
            _log.Info(message);
        }

        public void Info(string message, Exception ex)
        {
            var msg = string.Format("消息：{0}信息{1},堆栈{2},{3}", message, ex.Message, ex.StackTrace, (ex.InnerException == null ? "" : ex.InnerException.Message));

            _log.Info(msg);
        }

        public void Debug(string message)
        {
            _log.Debug(message);
        }

        public void Debug(string message, Exception ex)
        {
            _log.Debug(message, ex);
        }
    }
}
